package com.itheima.consumer.dao;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: homechen
 * Date: 2024-04-27
 * Time: 9:47
 */

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建延迟交换机 以及延迟交换机
 */
@Configuration
public class delayExchange {
    public static final String EXCHANGE_NAME = "myDelayedExchange.delay";
    public static final String QUEUE_NAME = "delayed.queue";
    public static final String ROUNTING_KEY = "delayed-routing-key";

    // 延迟交换机
    @Bean
    public CustomExchange delayedExchange() {
        return new CustomExchange(EXCHANGE_NAME,
                "x-delayed-message",  // 消息类型
                true,   // 持久化
                false);
    }

    // 延迟队列
    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(QUEUE_NAME).withArgument("x-delayed-type", "direct").build();
    }

    // 绑定延迟交换机和队列
    @Bean
    public Binding delayBinding(Queue delayedQueue, CustomExchange delayedExchange) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(ROUNTING_KEY).noargs();
    }
}
